package com.rayframework.mybatisplus.extension.plugins.pagination;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Ray框架分页对象
 *
 * @author LiaoYuXing-Ray
 * @version 1.0
 * @createDate 2023/10/12 14:54
 **/
public class Page<T> implements IPage<T> {

    private static final long serialVersionUID = -8016512249197662291L;
    /**
     * 数据 2023/10/12 15:47
     */
    protected List<T> records = Collections.emptyList();
    /**
     * 预留 - mybatisPlus 但是暂时没用
     * 2023/10/12 17:58
     */
    protected List<OrderItem> orders = new ArrayList<>();
    /**
     * 总记录数，默认 0 2023/10/12 15:01
     */
    protected long total = 0;
    /**
     * 每页显示条数，默认 10 2023/10/12 15:00
     */
    protected long size = 10;
    /**
     * 当前页，默认 1 2023/10/12 14:58
     */
    protected long current = 1;

    /**
     * 自动优化 COUNT SQL
     */
    protected boolean optimizeCountSql = true;
    /**
     * 是否进行 count 查询
     */
    protected boolean searchCount = true;
    /**
     * 单页分页条数限制
     */
    protected Long maxLimit;
    /**
     * countId
     */
    protected String countId;

    public Page() {
    }

    /**
     * 分页构造函数 - 【常用】
     *
     * @param current 当前页
     * @param size    每页显示条数
     */
    public Page(long current, long size) {
        this(current, size, 0);
    }

    public Page(long current, long size, long total) {
        this(current, size, total, true);
    }

    public Page(long current, long size, boolean searchCount) {
        this(current, size, 0, searchCount);
    }

    public Page(long current, long size, long total, boolean searchCount) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
        this.searchCount = searchCount;
    }

    public void setMaxLimit(Long maxLimit) {
        this.maxLimit = maxLimit;
    }

    public void setCountId(String countId) {
        this.countId = countId;
    }

    /**
     * 是否存在上一页
     *
     * @return true / false
     */
    public boolean hasPrevious() {
        return this.current > 1;
    }

    /**
     * 是否存在下一页
     *
     * @return true / false
     */
    public boolean hasNext() {
        return this.current < this.getPages();
    }

    @Override
    public long getPages() {
        // 解决 github issues/3208
        return IPage.super.getPages();
    }


    @Override
    public List<T> getRecords() {
        return this.records;
    }

    @Override
    public IPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public List<OrderItem> orders() {
        return this.orders;
    }


    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public IPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public IPage<T> setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public IPage<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    @Override
    public String countId() {
        return this.countId;
    }

    @Override
    public Long maxLimit() {
        return this.maxLimit;
    }

    @Override
    public boolean searchCount() {
        if (total < 0) {
            return false;
        }
        return searchCount;
    }
}
